package com.handu.luna.core.domain.promotion;


import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@MappedSuperclass
public class BonusPromotion extends Promotion {

    @Override
    public String getStatus() {
        return initStatus(this.getUseStartDate(), this.getUseEndDate());
    }

    public BonusPromotion() {
    }

    public BonusPromotion(String name,GrantType grantType, String useRanges, BigDecimal bonusPrice, BigDecimal limitPrice, Date grantStartDate, Date grantEndDate, Date useStartDate, Date useEndDate, String brandIds, String remark) {
        this.name = name;
        this.grantType = grantType;
        this.useRanges = useRanges;
        this.bonusPrice = bonusPrice;
        this.limitPrice = limitPrice;
        this.grantStartDate = grantStartDate;
        this.grantEndDate = grantEndDate;
        this.useStartDate = useStartDate;
        this.useEndDate = useEndDate;
        this.brandIds = brandIds;
        this.remark = remark;
    }


    //优惠劵/红包 发放类型
    public enum GrantType{
        USER("按用户发放"),GOOD("按商品发放"),ORDER("按订单发放"),UNDERLINE("线下发放");
        public String description;
        GrantType(final String description) {
         this.description = description;
        }
    }

    //优惠劵/红包 使用范围
    public enum UseRange{
        WEBSITE("官网"),PHONE("手机"),WAP("wap");
        public String description;
        UseRange(final String description) {
            this.description = description;
        }

        public static UseRange valueOf(int ordinal) {
            if (ordinal < 0 || ordinal >= values().length) {
                throw new IndexOutOfBoundsException("Invalid ordinal");
            }
            return values()[ordinal];
        }
    }


    //红包金额
    @NotNull(message = "金额不能为空")
    @Min(value = 0, message = "金额不能小于0")
    @Max(value = 99999999, message = "金额不能大于99999999")
    protected BigDecimal bonusPrice;
    //最小订单金额
    @NotNull(message = "最小订单金额不能为空")
    @Min(value = 0,message = "最小订单金额不能小于0")
    @Max(value = 99999999,message = "最小订单金额不能大于99999999")
    protected BigDecimal limitPrice;
    //发放类型
    protected GrantType grantType;
    //发放开始时间
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date grantStartDate;
    //发放结束时间
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date grantEndDate;
    //使用开始时间
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date useStartDate;
    //使用结束时间
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date useEndDate;
    //使用范围
    protected String useRanges;
    //可使用品牌ID列表
    protected String brandIds;
    //备注
    @Size(min = 0,max = 250,message = "使用规则的字符个数为0到250")
    protected String remark;
    //发放数量
    protected Long grantNum = 0L;
    //使用数量
    protected Long usedNum = 0L;
    //商品货号列表
    protected String goodsIds;
    //发放优惠券最小订单金额

    @Min(value = 0,message = "发放最小订单金额不能小于0")
    protected BigDecimal grantLimitPrice;

    @Transient
    public List<UseRange> useRangeList;
    @Transient
    protected List<Long> brandIdList;

    /**
     * 数据库到视图的字段转化
     * @param bonusPromotion
     */
    public void fromDateBaseToView(BonusPromotion bonusPromotion) {
        bonusPromotion.setUseRangeList(getUseRangeListFromString(bonusPromotion.getUseRanges()));
        bonusPromotion.setBrandIdList(getBandIdListFromString(bonusPromotion.getBrandIds()));
    }

    /**
     * 视图到数据库的字段转化
     * @param bonusPromotion
     */
    public void fromViewToDateBase(BonusPromotion bonusPromotion) {
        bonusPromotion.setBrandIds(getStringFromBrandIdList(bonusPromotion.getBrandIdList()));
        bonusPromotion.setUseRanges(getStringFromUseRangeList(bonusPromotion.getUseRangeList()));
    }

    /**
     * 将数据库存储的枚举字符串序列转换为List
     * @param useRanges
     * @return
     */
    public List<UseRange> getUseRangeListFromString(String useRanges) {
        List<UseRange> useRangeList = new ArrayList<>();
        if(StringUtils.isBlank(useRanges)) {
            return useRangeList;
        }
        String[] strArr = useRanges.split(",");
        for(String str : strArr) {
            try {
                int useRangeIndex = Integer.parseInt(str);
                useRangeList.add(UseRange.valueOf(useRangeIndex));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return useRangeList;
    }

    /**
     * 将数据库存储的品牌ID字符串序列转换为IdList
     * @param brandIds
     * @return
     */
    public List<Long> getBandIdListFromString(String brandIds) {
        List<Long> brandIdList = new ArrayList<>();
        if(StringUtils.isBlank(brandIds)) {
            return brandIdList;
        }
        String[] strArr = brandIds.split(",");
        for(String str : strArr) {
            try {
                Long brandId = Long.parseLong(str);
                brandIdList.add(brandId);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return brandIdList;
    }

    /**
     * 将brangIdList转换为数据库存储的字符序列，以“，”分隔
     * @param brandIdList
     * @return
     */
    public String getStringFromBrandIdList(List<Long> brandIdList) {
        if(null == brandIdList || brandIdList.size() == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for(int i=0; i<brandIdList.size(); i++) {

            sb.append(brandIdList.get(i).toString());
            if(i==brandIdList.size()-1) {
                break;
            }
            sb.append(",");
        }
        return sb.toString();
    }

    /**
     * 将useRangeList转换为数据库存储的字符序列，以“，”分隔
     * @param useRangeList
     * @return
     */
    public String getStringFromUseRangeList(List<UseRange> useRangeList) {
        if(null == useRangeList || useRangeList.size() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<useRangeList.size(); i++) {

            sb.append(useRangeList.get(i).ordinal());
            if(i==useRangeList.size()-1) {
                break;
            }
            sb.append(",");
        }
        return sb.toString();
    }

    public Long getGrantNum() {
        return grantNum;
    }

    public void setGrantNum(Long grantNum) {
        this.grantNum = grantNum;
    }

    public Long getUsedNum() {
        return usedNum;
    }

    public void setUsedNum(Long usedNum) {
        this.usedNum = usedNum;
    }

    public BigDecimal getBonusPrice() {
        return bonusPrice;
    }

    public void setBonusPrice(BigDecimal bonusPrice) {
        this.bonusPrice = bonusPrice;
    }

    public BigDecimal getLimitPrice() {
        return limitPrice;
    }

    public void setLimitPrice(BigDecimal limitPrice) {
        this.limitPrice = limitPrice;
    }

    public Date getGrantStartDate() {
        return grantStartDate;
    }

    public void setGrantStartDate(Date grantStartDate) {
        this.grantStartDate = grantStartDate;
    }

    public GrantType getGrantType() {
        return grantType;
    }

    public void setGrantType(GrantType grantType) {
        this.grantType = grantType;
    }

    public Date getGrantEndDate() {
        return grantEndDate;
    }

    public void setGrantEndDate(Date grantEndDate) {
        this.grantEndDate = grantEndDate;
    }

    public Date getUseStartDate() {
        return useStartDate;
    }

    public void setUseStartDate(Date useStartDate) {
        this.useStartDate = useStartDate;
    }

    public Date getUseEndDate() {
        return useEndDate;
    }

    public void setUseEndDate(Date useEndDate) {
        this.useEndDate = useEndDate;
    }

    public String getUseRanges() {
        return useRanges;
    }

    public void setUseRanges(String useRanges) {
        this.useRanges = useRanges;
    }

    public String getBrandIds() {
        return brandIds;
    }

    public void setBrandIds(String brandIds) {
        this.brandIds = brandIds;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<UseRange> getUseRangeList() {
        return useRangeList;
    }

    public void setUseRangeList(List<UseRange> useRangeList) {
        this.useRangeList = useRangeList;
    }

    public List<Long> getBrandIdList() {
        return brandIdList;
    }

    public void setBrandIdList(List<Long> brandIdList) {
        this.brandIdList = brandIdList;
    }

    public String getGoodsIds() {
        return goodsIds;
    }

    public void setGoodsIds(String goodsIds) {
        this.goodsIds = goodsIds;
    }

    public BigDecimal getGrantLimitPrice() {
        return grantLimitPrice;
    }

    public void setGrantLimitPrice(BigDecimal grantLimitPrice) {

        this.grantLimitPrice = grantLimitPrice;
    }
}
